<template>
	<view :style="{ height: conentHeight + 'px;' }">
		<view class="top-height">
			<view class="page-top-box">
				<view class="search-box flex ac">
					<u-search
						borderColor="#DADADA"
						bgColor="#ffffff"
						placeholder="搜索项目名称"
						v-model="searchForm.project_name"
						:showAction="false"
						@search="searchFunc"
						@clear="searchFunc"
					></u-search>
					<view class="search-btn flex ac" @click="searchShow = true" :style="searchShow ? 'color:#3B75FF;' : ''">
						筛选
						<text class="iconfont icon-shaixuan" :style="searchShow ? 'color:#3B75FF;' : ''"></text>
					</view>
					<u-button
						v-if="$isPer(per_arr, ['rectificationForm_add'])"
						@click="navToAddFunc"
						customStyle="width: 156rpx;height: 64rpx;background: #3B75FF;border-radius: 32rpx;border-color:#F8F8FA;font-weight: 400;font-size: 26rpx;color: #ffffff;margin-left:20rpx;"
						:hairline="false"
						text="发起整改单"
					></u-button>
				</view>
			</view>
		</view>
		<view class="list-content-box" :style="{ height: 'calc(100% - ' + topHeight + 'px);' }">
			<z-paging
				@onRefresh="refresh"
				ref="paging"
				:fixed="false"
				v-model="dataList"
				@query="queryList"
				:paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }"
			>
				<template slot="empty">
					<u-empty icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" text=" "></u-empty>
				</template>
				<view class="list-items flex ac flex-wrap" :key="index" v-for="(item, index) in dataList" @click.stop="navToDetailFunc(item)">
					<view class="list-items-title flex ac jb" v-if="$fieldShowFunc('zgd_xmmc')">
						<view class="title u-line-1">{{ item.project_name }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('zgd_xmbh')">
						<view class="title">项目编号</view>
						<view class="desc u-line-1">{{ item.project_number }}</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('zgd_zgqy')">
						<view class="title">整改区域</view>
						<view class="desc u-line-1">
							<template v-if="item.rectification_area == 1">前区</template>
							<template v-if="item.rectification_area == 2">后区</template>
							<template v-if="item.rectification_area == 3">仓库</template>
						</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('zgd_zglx')">
						<view class="title">整改类型</view>
						<view class="desc u-line-1">
							<template v-if="item.rectification_type == 1">水电</template>
							<template v-if="item.rectification_type == 2">吊顶</template>
							<template v-if="item.rectification_type == 3">门头</template>
						</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('zgd_sfyxky')">
						<view class="title">是否影响开业</view>
						<view class="desc u-line-1">
							<template v-if="item.is_affect_opening == 1">是</template>
							<template v-if="item.is_affect_opening == 2">否</template>
						</view>
					</view>
					<view class="list-items-item flex ac list-items-item-width50" v-if="$fieldShowFunc('zgd_zgjg')">
						<view class="title">整改结果</view>
						<view class="desc u-line-1">
							<template v-if="item.rectification_status == 10">待整改</template>
							<template v-if="item.rectification_status == 20">审核中</template>
							<template v-if="item.rectification_status == 30">已完成</template>
						</view>
					</view>
					<!-- 按钮 -->
					<view style="width: 100%">
						<judgeBtn plain :per_arr="per_arr" :btns="btns" :state="item.rectification_status" :item="item" @change="btnsChange"></judgeBtn>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 筛选弹窗 -->
		<u-popup :show="searchShow" round="30" @close="searchShow = false" closeable>
			<view class="search-popup-title">全部筛选</view>
			<scroll-view scroll-y class="search-popup-box">
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>整改区域</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('rectification_area', item)"
							class="search-items-list-item"
							:class="item.value == searchForm.rectification_area ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.rectification_area.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>整改类型</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('rectification_type', item)"
							class="search-items-list-item"
							:class="item.value == searchForm.rectification_type ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.rectification_type.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">整改内容</view>
					<u--input placeholder="整改内容" border="surround" v-model="searchForm.rectification_content"></u--input>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>是否影响开业</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('is_affect_opening', item)"
							class="search-items-list-item"
							:class="item.value == searchForm.is_affect_opening ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.is_affect_opening.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">限期整改时间</view>
					<view @click="chooseDateFunc">
						<u--input readonly placeholder="限期整改时间" border="surround" :value="searchForm.deadline_rectification_show"></u--input>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">整改结果</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('rectification_status', item)"
							class="search-items-list-item"
							:class="item.value == searchForm.rectification_status ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.rectification_status.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="btn-box flex ac jb">
				<u-button
					@click="reSearchValFunc"
					text="重置"
					customStyle="width: 248rpx;height: 82rpx;background: #242834;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
				<u-button
					@click="searchFunc"
					text="查询"
					customStyle="width: 422rpx;height: 82rpx;background: #3B75FF;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
			</view>
		</u-popup>
		<!-- 创建时间筛选 -->
		<u-calendar
			:show="dateShow"
			mode="range"
			@confirm="dateConfirmFunc"
			@close="dateShow = false"
			:maxDate="maxDate"
			:minDate="minDate"
			:defaultDate="defaultDate"
			:monthNum="36"
		></u-calendar>
		<!-- 操作组件 -->
		<engineeringRectificationOperate :value.sync="per_name" :oper_item="oper_item" @submit="handleSubmit"></engineeringRectificationOperate>
	</view>
</template>

<script>
import search from '../../../../uni_modules/uview-ui/libs/config/props/search';

export default {
	name: 'engineeringRectification',
	props: {
		conentHeight: {
			default: 0
		},
		option: {
			type: Object,
			default: {}
		}
	},
	data() {
		const d = new Date();
		const year = d.getFullYear();
		let month = d.getMonth() + 1;
		month = month < 10 ? `0${month}` : month;
		const date = d.getDate();
		return {
			defaultDate: [`${year}-${month}-${date}`],
			maxDate: `${year}-${month}-${date}`, //日历最大日期
			minDate: `${year - 2}-${month}-${date}`, //日历最小日期
			dateShow: false,
			searchShow: false,
			dataList: [],
			searchForm: {
				project_name: '',
				rectification_area: '',
				rectification_type: '',
				rectification_content: '',
				is_affect_opening: '',
				deadline_rectification_show: '',
				deadline_rectification_start: '',
				deadline_rectification_end: '',
				rectification_status: ''
			},
			topHeight: 0,
			loading: false,
			searchListObj: {
				rectification_area: {
					list: [
						{
							label: '前区',
							value: 1
						},
						{
							label: '后区',
							value: 2
						},
						{
							label: '仓库',
							value: 3
						}
					],
					page: 1,
					num: 5,
					max: 0
				},
				rectification_type: {
					list: [
						{
							label: '水电',
							value: 1
						},
						{
							label: '吊顶',
							value: 2
						},
						{
							label: '门头',
							value: 3
						}
					],
					page: 1,
					num: 5,
					max: 0
				},
				is_affect_opening: {
					list: [
						{
							label: '是',
							value: 1
						},
						{
							label: '否',
							value: 2
						}
					],
					page: 1,
					num: 5,
					max: 0
				},
				rectification_status: {
					list: [
						{
							label: '待整改',
							value: 10
						},
						{
							label: '审核中',
							value: 20
						},
						{
							label: '未完成',
							value: 40
						},
						{
							label: '已完成',
							value: 30
						}
					],
					page: 1,
					num: 5,
					max: 0
				}
			},
			per_arr: [], //权限列表
			// 操作按钮
			btns: [
				{
					per_name: 'rectificationForm_submit',
					state: 10,
					name: '上传整改内容'
				},
				{
					per_name: 'rectificationForm_examine',
					state: 20,
					name: '审核整改单'
				}
			],
			// 操作相关
			per_name: '',
			oper_item: {}
		};
	},
	mounted() {
		this.getHeight();
		// 获取权限
		this.$Api
			.getHtmlAuthority({
				path: '/rectificationForm'
			})
			.then((res) => {
				console.log(res, 'getHtmlAuthority');
				if (res.code == 200) {
					this.per_arr = res.data.permission;
				}
			});
	},
	watch: {},
	methods: {
		// 刷新修改当前操作item
		refreshItemFunc() {
			console.log('刷新当前数据');
			let oper_item = this.oper_item;
			let index = this.dataList.findIndex((item) => {
				return item.id == oper_item.id;
			});
			if (index == -1) return;
			this.$Apipc
				.getRectificationDetails({
					rectification_id: oper_item.id
				})
				.then((res) => {
					console.log(res, '项目详情');
					if (res.code == 200) {
						this.dataList[index] = res.data;
						this.dataList.splice(0, 0);
					}
				});
		},
		// 操作反馈
		handleSubmit(e) {
			this.refreshItemFunc();
		},
		// 操作事件
		btnsChange(e) {
			console.log(e);
			this.per_name = e.per_name;
			this.oper_item = e.item;
		},
		refresh() {
			this.dataList = [];
		},
		// 重置弹窗筛选项
		reSearchValFunc() {
			this.searchForm = {
				project_name: '',
				rectification_area: '',
				rectification_type: '',
				rectification_content: '',
				is_affect_opening: '',
				deadline_rectification_show: '',
				deadline_rectification_start: '',
				deadline_rectification_end: '',
				rectification_status: ''
			};
			this.$nextTick(() => {
				this.searchFunc();
			});
		},
		// 刷新列表
		searchFunc() {
			this.$refs.paging.reload();
			this.searchShow = false;
		},
		getHeight() {
			// 创建节点查询
			let that = this;
			const systemInfo = uni.getSystemInfoSync();
			const query = uni.createSelectorQuery().in(that);
			query.select('.top-height').boundingClientRect();
			query.exec((res) => {
				that.topHeight = res[0].height + systemInfo.statusBarHeight + 20;
				console.log(that.topHeight, res[0].height);
				setTimeout(() => {
					that.loading = true;
				}, 0);
			});
		},
		// 去详情
		navToDetailFunc(item) {
			this.oper_item = item;
			uni.navigateTo({
				url: '/pagesB/engineeringRectificationDetail/engineeringRectificationDetail?id=' + item.id
			});
		},
		queryList(page, num) {
			this.getRectificationList(page, num);
		},
		// 施工列表
		getRectificationList(page, num) {
			let searchForm = this.searchForm;
			uni.showLoading({
				mask: true
			});
			this.$Apipc
				.getRectificationList({
					page,
					num,
					project_name: searchForm.project_name,
					rectification_area: searchForm.rectification_area,
					rectification_type: searchForm.rectification_type,
					rectification_content: searchForm.rectification_content,
					is_affect_opening: searchForm.is_affect_opening,
					deadline_rectification_start: searchForm.deadline_rectification_start,
					deadline_rectification_end: searchForm.deadline_rectification_end,
					rectification_status: searchForm.rectification_status
				})
				.then((res) => {
					uni.hideLoading();
					console.log(res, '工程整改列表');
					if (res.code == 200) {
						this.$refs.paging.complete(res.data.list);
					}
				});
		},

		// 弹窗筛选项
		searchSelectFunc(type, item) {
			console.log(type, item);
			switch (type) {
				case 'rectification_area':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				case 'rectification_type':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				case 'is_affect_opening':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				case 'rectification_status':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				default:
			}
		},
		// 创建时间日历弹窗
		chooseDateFunc() {
			this.dateShow = true;
		},
		// 日期筛选确认
		dateConfirmFunc(e) {
			this.searchForm.deadline_rectification_start = e[0];
			this.searchForm.deadline_rectification_end = e[e.length - 1];
			this.searchForm.deadline_rectification_show = e[0] + '~' + e[e.length - 1];
			this.dateShow = false;
		},
		// 发起整改单
		navToAddFunc() {
			uni.navigateTo({
				url: '/pagesC/engineeringManagement/addRectificationForm/addRectificationForm'
			});
		}
	}
};
</script>

<style lang="scss"></style>
